package com.fudan.es.aggs.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fudan.biz.data.service.FieldMetaService;
import com.fudan.es.entity.FieldMeta;
import com.fudan.framework.utils.SpringUtils;

public class ChartResponseEntity {

	private String requestJson;
	private List<FieldMeta> fieldList;
	private String realRequest;
	private String realResponse;
	private Map<String,Object> responseData;

	private List<Map<String,Object>> listData;
	public List<FieldMeta> getFieldList() {
		return fieldList;
	}
	public void setFieldList(List<FieldMeta> fieldList) {
		this.fieldList = fieldList;
	}
	public String getRealRequest() {
		return realRequest;
	}
	public void setRealRequest(String realRequest) {
		this.realRequest = realRequest;
	}
	public String getRealResponse() {
		return realResponse;
	}
	public void setRealResponse(String realResponse) {
		this.realResponse = realResponse;
	}
	public Map<String, Object> getResponseData() {
		return responseData;
	}
	public void setResponseData(Map<String, Object> responseData) {
		this.responseData = responseData;
		this.fieldList = new LinkedList<>();
		List<Map<String,Object>> listMap = new ArrayList<>();
		fieldCache = new HashSet<>();
		flatData(listMap,null,this.responseData);
		this.listData = listMap;
	}
	public List<Map<String,Object>> getListData() {
		return listData;
	}
	public void setListData(List<Map<String,Object>> listData) {
		this.listData = listData;
	}

	private Set<String> fieldCache;
	private void flatData(List<Map<String,Object>> flatMap,Map<String,Object> need,Map<String,Object> data) {
		Set<String> keySet = data.keySet(); //第一层返回值，这里一定是id
		for (String key : keySet) { //id
			Object subObj = data.get(key); //该id的返回
			if(!Map.class.isAssignableFrom(subObj.getClass())) {
				continue;
			}
			if(!fieldCache.contains(key)) {
				fieldCache.add(key);
				this.fieldList.add(buildFieldMeta(key));
			}
			Map<String,Object> sub = (Map<String, Object>) data.get(key);
			Set<String> subKeySet = sub.keySet();
			for (String subKey : subKeySet) {
				Map<String,Object> item = new HashMap<>();
				Object v = sub.get(subKey);
				if(Map.class.isAssignableFrom(v.getClass())) {
					item.put(key, subKey);
					if(need != null) {
						item.putAll(need);
					}
					Map<String,Object> subMap = (Map<String, Object>) sub.get(subKey);
					Set<String> next = subMap.keySet();
					boolean end = true;
					for (String nk : next) {
						if(nk.matches("[0-9]+")) {
							end = false;
							break;
						}
					}
					if(end) {
						item.putAll(subMap);
						flatMap.add(item);
					}else {
						flatData(flatMap,item,subMap);
					}
				}else {
					if(need != null) {
						item.putAll(need);
					}
					item.putAll(sub);
					flatMap.add(item);
				}

			}
		}
	}

	private FieldMeta buildFieldMeta(String id) {
		FieldMetaService fieldMetaService = SpringUtils.getBean(FieldMetaService.class);
		return fieldMetaService.get(id);
	}
	public String getRequestJson() {
		return requestJson;
	}
	public void setRequestJson(String requestJson) {
		this.requestJson = requestJson;
	}
}
